using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
        private List<WPMENU> MapWPMENU(XElement item2, bool mergedb)
        {
            List<WPMENU> result = new List<WPMENU>();
            var item4 = from item3 in item2.Elements()
                        select new WPMENU
                        {
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            MENUID = (string)item3.Element("MENUID"),
                            MENUNAME = (string)item3.Element("MENUNAME"),
                            MENUTAG = (string)item3.Element("MENUTAG"),
                            MENUPAGE = (string)item3.Element("MENUPAGE"),
                            Photo = (string)item3.Element("Photo")
                        };

            result = item4.ToList();
            MapWPMENU(result, mergedb); 
            return result;
        }
        public void MapWPMENU(List<WPMENU> result, bool mergedb)
        {
            if (mergedb)
            {
                var rowsexist = from u in LocalDB.WPMENU.ToList().AsQueryable()
                                join uc in result.AsQueryable() on u.MAINKEY equals uc.MAINKEY
                                select u;
                LocalDB.WPMENU.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.WPMENU.InsertAllOnSubmit(result);
                LocalDB.SubmitChanges();
            }
        }
    }
    [Table(Name = "WPMENU")]
    public class WPMENU : INotifyPropertyChanged, INotifyPropertyChanging
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    NotifyPropertyChanging("MAINKEY");
                    _MAINKEY = value;
                    NotifyPropertyChanged("MAINKEY");
                }
            }
        }
        [Column(IsVersion = true)]
        private Binary _version;

        private string _MENUID = "0";
        [Column]
        public string MENUID
        {
            get
            {
                return _MENUID;
            }
            set
            {
                if (value != _MENUID)
                {
                    NotifyPropertyChanging("MENUID");
                    _MENUID = value;
                    NotifyPropertyChanged("MENUID");
                }
            }
        }
        private string _MENUNAME = "0";
        [Column]
        public string MENUNAME
        {
            get
            {
                return _MENUNAME;
            }
            set
            {
                if (value != _MENUNAME)
                {
                    NotifyPropertyChanging("MENUNAME");
                    _MENUNAME = value;
                    NotifyPropertyChanged("MENUNAME");
                }
            }
        }
        private string _MENUTAG = "0";
        [Column]
        public string MENUTAG
        {
            get
            {
                return _MENUTAG;
            }
            set
            {
                if (value != _MENUTAG)
                {
                    NotifyPropertyChanging("MENUTAG");
                    _MENUTAG = value;
                    NotifyPropertyChanged("MENUTAG");
                }
            }
        }
        private string _MENUPAGE = "0";
        [Column]
        public string MENUPAGE
        {
            get
            {
                return _MENUPAGE;
            }
            set
            {
                if (value != _MENUPAGE)
                {
                    NotifyPropertyChanging("MENUPAGE");
                    _MENUPAGE = value;
                    NotifyPropertyChanged("MENUPAGE");
                }
            }
        }
        public void ConvertToFile(IsolatedStorageFile ISO, ObservableCollection<WPMENU> ListInput, string filep = "", string setguid = "")
        {
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\WPMENU\\WPMENU" + setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\WPMENU";
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("WPMENU");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX" + setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (WPMENU row in ListInput)
                {
                    XElement f = new XElement("WPMENU");
                    XElement MAINKEY = new XElement("MAINKEY");
                    f.Add(MAINKEY);

                    XElement MENUID = new XElement("MENUID");
                    MENUID.Value = row.MENUID;
                    f.Add(MENUID);
                    XElement MENUNAME = new XElement("MENUNAME");
                    MENUNAME.Value = row.MENUNAME;
                    f.Add(MENUNAME);
                    XElement MENUTAG = new XElement("MENUTAG");
                    MENUTAG.Value = row.MENUTAG;
                    f.Add(MENUTAG);
                    XElement MENUPAGE = new XElement("MENUPAGE");
                    MENUPAGE.Value = row.MENUPAGE;
                    f.Add(MENUPAGE);
                    root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            {
            }

        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
        public bool IsFirst = false;
        private string _Photo = "YES";
        public string Photo
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    NotifyPropertyChanging("Photo");
                    _Photo = value;
                    NotifyPropertyChanged("Photo");
                    NotifyPropertyChanged("PhotoISOSource");
                }
            }
        }
        public string PhotoSource
        {
            get
            {
                string image = string.Format("/Data/Images/{0}", "YES");
                if (!Photo.Contains("http://"))
                {
                    return string.Format("/Data/Images/{0}", Photo);
                }
                return Photo;
            }
        }
        public BitmapImage PhotoISOSource
        {
            get
            {
                string image = string.Format("Data/PhoneDB/Pictures/{0}.jpg", Photo);
                BitmapImage bi = new BitmapImage();
                IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
                if (!iso.FileExists(image))
                    image = string.Format("Data/PhoneDB/Pictures/{0}.jpg", "YES");
                using (IsolatedStorageFileStream fileStream = iso.OpenFile(image, FileMode.Open, FileAccess.Read))
                {
                    //bi.SetSource(fileStream);
                }
                return bi;
            }
        }
    }
}
